package com.zds.support.api.retrofit.function;


import android.text.TextUtils;
import android.util.Log;
import android.util.MalformedJsonException;

import com.google.gson.JsonIOException;
import com.google.gson.JsonParseException;
import com.zds.support.R;
import com.zds.support.api.retrofit.exception.ApiException;
import com.zds.support.base.SupportBaseApplication;
import com.zds.support.util.AppUtils;

import java.io.IOException;
import java.net.ConnectException;
import java.net.SocketException;
import java.net.SocketTimeoutException;
import java.net.UnknownHostException;
import java.text.ParseException;

import javax.net.ssl.SSLException;

import retrofit2.HttpException;

public class ExceptionEngine {
    public static final int EMPTY_DATA = 999;   // 无数据
    public static final int UN_KNOWN_ERROR = 1000;   //未知错误
    public static final int ANALYTIC_SERVER_DATA_ERROR = 1001;   //解析(服务器)数据错误
    public static final int ANALYTIC_CLIENT_DATA_ERROR = 1002;   //解析(客户端)数据错误
    public static final int CONNECT_ERROR = 1003;   //网络连接错误
    public static final int TIME_OUT_ERROR = 1004;   //网络连接超时
    public static final int HTTP_ERROR = 1005;   // HttpException
    public static final int API_RESPONSE_ERROR = 1099;   // HttpException

    static String getStringById(int resId) {
        return AppUtils.appContext.getString(resId);
    }

    public static ApiException handleException(String url, Throwable e) {
        if (e instanceof ApiException) {
            if (TextUtils.equals("auth.no_auth", ((ApiException) e).getCode())) {
                SupportBaseApplication.Companion.getSupportBaseApplication()
                        .logout("auth.no_auth", getStringById(R.string.login_expired_login_again));
            }
            return (ApiException) e;
        }
        ApiException ex;
        String reportMsg = null;
        if (e instanceof HttpException) {
            HttpException httpException = (HttpException) e;
            int code = httpException.code();
            if (code == 502){
                ex = new ApiException(HTTP_ERROR, getStringById(R.string.server_exception_502));
            }else {
                ex = new ApiException(HTTP_ERROR, getStringById(R.string.server_exception) + ":" + code);
            }

        } else if (e instanceof JsonIOException || e instanceof JsonParseException
                || e instanceof ParseException || e instanceof MalformedJsonException
                || e instanceof com.google.gson.stream.MalformedJsonException) {  //解析数据错误
            ex = new ApiException(ANALYTIC_SERVER_DATA_ERROR, getStringById(R.string.parsing_error));
            reportMsg = "解析错误!" + e.getLocalizedMessage();
            if (AppUtils.isDebug) {
                e.printStackTrace();
            }
        } else if (e instanceof ConnectException || e instanceof SocketException) {//连接网络错误
            ex = new ApiException(CONNECT_ERROR, getStringById(R.string.network_error_please_check));
        } else if (e instanceof SocketTimeoutException) {//网络超时
            ex = new ApiException(TIME_OUT_ERROR, getStringById(R.string.network_error_please_check));
        } else if (e instanceof UnknownHostException) {
            ex = new ApiException(CONNECT_ERROR, getStringById(R.string.network_error_please_check));
        } else if (e instanceof IllegalArgumentException) {
            ex = new ApiException(UN_KNOWN_ERROR, getStringById(R.string.illegal_request_parameter));
        } else if (e instanceof SSLException) {
            ex = new ApiException(CONNECT_ERROR, getStringById(R.string.network_error_please_check)+"(-1)");
        }else if (e instanceof IOException) {
            ex = new ApiException(CONNECT_ERROR, getStringById(R.string.network_error_please_check));
        } else {  //未知错误
           /* if (e != null) {
                CrashReport.postCatchedException(e);
            }*/
            e.printStackTrace();
            reportMsg = "未知错误!  " + e.getLocalizedMessage();
            Log.e("错误", reportMsg);
            ex = new ApiException(UN_KNOWN_ERROR, getStringById(R.string.unknown_error) + "\n" + e.getLocalizedMessage());
            if (url == null || TextUtils.isEmpty(url)) {
                // AnalyticsManager.onApiFailEvent("", ex.getCode(), reportMsg);
            }
        }
        try {
            if (url != null && !TextUtils.isEmpty(url)) {
                if (reportMsg == null) {
                    reportMsg = ex.getMsg();
                }
                // AnalyticsManager.onApiFailEvent(url, ex.getCode(), reportMsg);
            }
        } catch (Exception e1) {

        }

        return ex;
    }
}
